home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #6 / Amiga Plus CD - 2004 - No. 06.iso / AmiSoft / Comm / misc / trsi-ftpd01.lha / FAME-FTPd / source / FTPDoor / main.c < prev    next >
C/C++ Source or Header  |  2004-04-24  |  8KB  |  260 lines

  1. /****************************************************************************************
  2.  *  PROJECT: FAME-FTPd / FTPDoor
  3.  *     FILE: main.c
  4.  *  PURPOSE: Main file of FTPDoor - Multi-Purpose FAME door for FTPd
  5.  *  CREATED: 31-AUG-2003
  6.  * MODIFIED: 24-APR-2004
  7.  *   AUTHOR: Sascha 'SieGeL' Pfalz
  8.  ****************************************************************************************/
  9.  
  10. #include <exec/exec.h>
  11. #include <Fame/fameDoorProto.h>
  12. #include <pragmas/utility_pragmas.h>
  13. #include <pragmas/date_pragmas.h>
  14. #include <proto/locale.h>
  15. #include <proto/date.h>
  16. #include <libraries/date.h>
  17. #include <utility/date.h>
  18. #include <utility/tagitem.h>
  19. #include "version.h"
  20. #include "/struct.h"
  21. #include "/global_defines.h"
  22.  
  23. #define    CMD_FTPWHO        0                // Who is online command (FTPWHO)
  24. #define CMD_FTPINIT        1                // Initial check for new files command (FTPINIT)
  25.  
  26. /****************************************************************************
  27.  * Global variables
  28.  ****************************************************************************/
  29.  
  30. const     char *ver="$VER: FTPDoor "COMPILE_VERSION" ("COMPILE_DATE") ["CPU_TYPE"]\0";
  31. static     char *doorcommands[]={"FTPWHO","FTPINIT",NULL};
  32. long        node;
  33.  
  34. /****************************************************************************
  35.  * PROTOTYPES
  36.  ****************************************************************************/
  37.  
  38. void main(void);
  39. void __autoopenfail(void) { _XCEXIT(0);}    // Dummy function for SAS
  40. void CloseLibs(void);
  41. void ShutDown(long error);
  42. void wb(char *err);
  43.  
  44. static void FTPWho(void);
  45. static void ShowWhoList(struct FTPdStats *, long entries);
  46.  
  47. extern void FTPInit(void);                                // Defined in init.c
  48.  
  49. /****************************************************************************
  50.  *  FUNCTION: main()
  51.  *   PURPOSE: Main entry of tool
  52.  * PARAMETER: none
  53.  *   RETURNS: none
  54.  ****************************************************************************/
  55.  
  56. void main(void)
  57.     {
  58.     struct     RDArgs *rda=NULL;
  59.     long    ArgArray[1]={0L};
  60.     char        iobuf[202],**lv;
  61.     int            cmd = 0,actcommand = -1;
  62.  
  63.     if(rda=ReadArgs("NODE-NR./A/N",ArgArray,rda))
  64.         {
  65.         node=*(LONG *)ArgArray[0];
  66.         SPrintf((STRPTR)FAMEDoorPort,"FAMEDoorPort%ld",node);
  67.         FreeArgs(rda);
  68.         }
  69.     else
  70.         {
  71.         FreeArgs(rda);
  72.         PrintFault(IoErr(),(FilePart(_ProgramName)));
  73.         Printf("\n%s is a FAME BBS-Door and is only usable via the BBS !\n\n",(FilePart(_ProgramName)));
  74.         exit(0);
  75.         }
  76.     if(!(mem_pool=CreatePool(MEMF_CLEAR|MEMF_PUBLIC,20480L,20480L))) { SetIoErr(ERROR_NO_FREE_STORE);CloseLibs();}
  77.     if(!(UtilityBase=(struct Library *)     OpenLibrary("utility.library",37L))) { SetIoErr(ERROR_INVALID_RESIDENT_LIBRARY);exit(20);}
  78.     if(!(FAMEBase=(struct FAMELibrary *)     OpenLibrary(FAMENAME,5L))) { SetIoErr(ERROR_INVALID_RESIDENT_LIBRARY);CloseLibs();}
  79.     if(!(LocaleBase=(struct LocaleBase *)    OpenLibrary("locale.library",38L))) { SetIoErr(ERROR_INVALID_RESIDENT_LIBRARY);CloseLibs();}
  80.     myloc=OpenLocale(NULL);
  81.     if(FIMStart(0,0UL)) CloseLibs();
  82.     if(!(fib = AllocDosObject(DOS_FIB,NULL)))
  83.         {
  84.         wb("CANNOT ALLOCATE FIB STRUCTURE!!!");
  85.         }
  86.     GetCommand(iobuf,0,0,0,NR_GetDoorCallName);
  87.   lv = doorcommands;
  88.     while(*lv!=NULL)
  89.         {
  90.     if(!Stricmp(iobuf,*lv))
  91.             {
  92.             actcommand = cmd;
  93.             break;
  94.             }
  95.         cmd++;
  96.         *lv++;
  97.         }
  98.     if(actcommand == -1)
  99.         {
  100.         PutStringFormat("\n\rFTPDoor: Unknown Callmode %s specified - Plz read docs!!\r\n",iobuf);
  101.         wb("\r\n");
  102.         }
  103.     switch(actcommand)
  104.         {
  105.         case  CMD_FTPWHO:        FTPWho();
  106.                                                 break;
  107.         case    CMD_FTPINIT:    FTPInit();
  108.                                                 break;
  109.         }
  110.     wb("");
  111.     }
  112. /****************************************************************************
  113.  *  FUNCTION: wb()
  114.  *   PURPOSE: Terminates the FAMEDoor session
  115.  * PARAMETER: none
  116.  *   RETURNS: none
  117.  ****************************************************************************/
  118.  
  119. void wb(char *err)
  120.     {
  121.     if(*err) NC_PutString(err,1);
  122.     PutStringFormat("\n\r");
  123.     FIMEnd(0);
  124.     }
  125.  
  126. /****************************************************************************
  127.  *   FUNCTION: ShutDown()
  128.  *    PURPOSE: Called by FAME Door header to shutdown door, calls CloseLibs()
  129.  * PARAMETERS: error => Error code from FAME, see FAMEDefines.h
  130.  *    RETURNS: none
  131.  ****************************************************************************/
  132.  
  133. void ShutDown(long error)
  134.     {
  135.     CloseLibs();
  136.     }
  137.  
  138. /****************************************************************************
  139.  *  FUNCTION: CloseLibs()
  140.  *   PURPOSE: Closes all libs and frees all resources
  141.  * PARAMETER: none
  142.  *   RETURNS: none
  143.  ****************************************************************************/
  144.  
  145. void CloseLibs(void)
  146.     {
  147.     if(fib)                        FreeDosObject(DOS_FIB,fib); fib = NULL;
  148.     if(UtilityBase)        CloseLibrary(UtilityBase); UtilityBase=NULL;
  149.   if(LocaleBase)
  150.         {
  151.         if(myloc) CloseLocale(myloc);myloc=NULL;
  152.         CloseLibrary((struct Library *)LocaleBase);LocaleBase=NULL;
  153.         };
  154.     if(FAMEBase)             CloseLibrary((struct Library *)FAMEBase); FAMEBase=NULL;
  155.     if(mem_pool)      DeletePool(mem_pool); mem_pool = NULL;
  156.     exit(0);
  157.     }
  158.  
  159. /****************************************************************************
  160.  *  FUNCTION: FTPWho()
  161.  *   PURPOSE: Handles FTPWho() Command
  162.  * PARAMETER: none
  163.  *   RETURNS: none
  164.  ****************************************************************************/
  165.  
  166. static void FTPWho(void)
  167.     {
  168.     struct  FTPdStats *mydata;
  169.     long    myentries,cnt = 0, i;
  170.     BPTR        fp;
  171.     static    char noconns[]="\n\rCurrently there are no active FTP connections.\n\r";
  172.     char        headbuf[200];
  173.  
  174.     PutString("\f\n\r",0);
  175.     SPrintf(headbuf,"FTPWho v%s by Sascha 'SieGeL/tRSi' Pfalz",COMPILE_VERSION);
  176.     Center(headbuf,1,35);
  177.     PutString("\n\r",1);
  178.     PutString("Username           |  Connection Time  Action",1);
  179.     PutString("-------------------+-------------------+-------------------------------------",1);
  180.     while(cnt < STATS_FILE_RETRY)
  181.         {
  182.         if(!(fp = Open(statname,MODE_OLDFILE)))        // File is accessed by many instances!
  183.             {
  184.             if(IoErr()==ERROR_OBJECT_NOT_FOUND)            // IoErr() = 205, this is okay and not treated as error
  185.                 {
  186.                 PutString(noconns,1);
  187.                 return;
  188.         }
  189.             cnt++;
  190.             Delay(STATS_RETRY_DELAY);
  191.             continue;
  192.             }
  193.         ExamineFH(fp,fib);
  194.         myentries = fib->fib_Size / sizeof(struct FTPdStats);
  195.         if(!myentries)                                                        // File has 0 entries, okay as it shows no active connections
  196.             {
  197.         PutString(noconns,1);
  198.             Close(fp);
  199.             return;
  200.             }
  201.         if(!(mydata = AllocPooled(mem_pool,sizeof(struct FTPdStats) * (myentries+1))))
  202.             {
  203.         PutString("\n\rCannot allocate memory for statistics??\n\r",1);
  204.             Close(fp);
  205.             return;
  206.             }
  207.     for(i = 0; i < myentries; i++)
  208.             {
  209.       if(!Read(fp,&mydata[i],sizeof(struct FTPdStats)))
  210.                 {
  211.         PutStringFormat("\r\nError reading statsfile: %ld\n\r",IoErr());
  212.                 Close(fp);
  213.                 return;
  214.                 }
  215.             }
  216.         Close(fp);
  217.         ShowWhoList(mydata,myentries);
  218.         return;
  219.         }
  220.     PutStringFormat("Error while accessing stats file: %ld\n\r",IoErr());        // This should never happen!
  221.     }
  222.  
  223. /****************************************************************************
  224.  *  FUNCTION: ShowWhoList()
  225.  *   PURPOSE: Displays list of active users
  226.  * PARAMETER: none
  227.  *   RETURNS: none
  228.  ****************************************************************************/
  229.  
  230. static void ShowWhoList(struct FTPdStats *dat, long entries)
  231.     {
  232.   long    i;
  233.     struct    FAMEUser *fuser;
  234.     struct    FAMEUserKeys *fkeys;
  235.  
  236.     if(!(fuser = AllocPooled(mem_pool, sizeof(struct FAMEUser))))
  237.         {
  238.     wb("No memory left for user.data structure?");
  239.         }
  240.     if(!(fkeys = AllocPooled(mem_pool, sizeof(struct FAMEUserKeys))))
  241.         {
  242.     wb("No memory left for user.keys structure?");
  243.         }
  244.     for(i = 0; i < entries; i++)
  245.         {
  246.         if(dat[i].UserNumber)
  247.             {
  248.             MyFAMEDoorMsg->fdom_StructDummy1 = fuser;
  249.             MyFAMEDoorMsg->fdom_StructDummy2 = fkeys;
  250.             GetCommand("",dat[i].UserNumber,0,0,RD_LoadAccount);
  251.             }
  252.         else
  253.             {
  254.       FAMEStrCopy("N/A!",fuser->UserName,31);
  255.             }
  256.         PutStringFormat("%-18s %17s %-36s\n\r",fuser->UserName,&dat[i].ConnectTime,&dat[i].ActionString);
  257.         }
  258.     }
  259.  
  260.